Don't conditionally compile escaping strategies
authorAlex Crichton <alex@alexcrichton.com>
Mon, 29 Jun 2015 16:22:49 +0000 (09:22 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 29 Jun 2015 16:22:49 +0000 (09:22 -0700)
Compile them both in and select at runtime.

src/cargo/util/process_builder.rs
src/cargo/util/shell_escape.rs

index 2411424f535d0193ba93c9eb888fd75c2f0f9cbc..628b681c1f1dad0877938a19adc220269d44affb 100644 (file)
@@ -6,7 +6,7 @@ use std::path::Path;
 use std::process::{Command, Output};
 
 use util::{CargoResult, ProcessError, process_error};
-use util::shell_escape::shell_escape;
+use util::shell_escape::escape;
 
 #[derive(Clone, PartialEq, Debug)]
 pub struct ProcessBuilder {
@@ -21,7 +21,7 @@ impl fmt::Display for ProcessBuilder {
         try!(write!(f, "`{}", self.program.to_string_lossy()));
 
         for arg in self.args.iter() {
-            try!(write!(f, " {}", shell_escape(arg.to_string_lossy())));
+            try!(write!(f, " {}", escape(arg.to_string_lossy())));
         }
 
         write!(f, "`")
index d65f42556ce24f595f282891544a43b2ec572341..f2694254c5d50de7bdfcb614a081c81412979e94 100644 (file)
@@ -8,12 +8,17 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-#[cfg(target_os = "windows")]
-pub use self::windows::shell_escape;
+use std::borrow::Cow;
 
+pub fn escape(s: Cow<str>) -> Cow<str> {
+    if cfg!(unix) {
+        unix::escape(s)
+    } else {
+        windows::escape(s)
+    }
+}
 
-#[cfg(any(test, target_os = "windows"))]
-mod windows {
+pub mod windows {
     use std::borrow::Cow;
 
     const SPACE: char = ' ';
@@ -28,7 +33,7 @@ mod windows {
     /// Escape double quotes and spaces by wrapping the string in double quotes.
     ///
     /// Turn all backslashes into forward slashes.
-    pub fn shell_escape(s: Cow<str>) -> Cow<str> {
+    pub fn escape(s: Cow<str>) -> Cow<str> {
         // check if string needs to be escaped
         let mut has_spaces = false;
         let mut has_backslashes = false;
@@ -63,29 +68,25 @@ mod windows {
     }
 
     #[test]
-    fn test_shell_escape() {
-        assert_eq!(shell_escape("--aaa=bbb-ccc".into()), "--aaa=bbb-ccc");
-        assert_eq!(shell_escape("linker=gcc -L/foo -Wl,bar".into()),
-                                r#""linker=gcc -L/foo -Wl,bar""#);
-        assert_eq!(shell_escape(r#"--features="default""#.into()),
-                                r#"--features=\"default\""#);
-        assert_eq!(shell_escape(r#"\path\to\my documents\"#.into()),
-                                r#""/path/to/my documents/""#);
+    fn test_escape() {
+        assert_eq!(escape("--aaa=bbb-ccc".into()), "--aaa=bbb-ccc");
+        assert_eq!(escape("linker=gcc -L/foo -Wl,bar".into()),
+                          r#""linker=gcc -L/foo -Wl,bar""#);
+        assert_eq!(escape(r#"--features="default""#.into()),
+                          r#"--features=\"default\""#);
+        assert_eq!(escape(r#"\path\to\my documents\"#.into()),
+                          r#""/path/to/my documents/""#);
     }
 }
 
-#[cfg(not(target_os = "windows"))]
-pub use self::other::shell_escape;
-
-#[cfg(any(test, not(target_os = "windows")))]
-mod other {
+pub mod unix {
     use std::borrow::Cow;
 
-    static SHELL_SPECIAL: &'static str = r#" \$'"`!"#;
+    const SHELL_SPECIAL: &'static str = r#" \$'"`!"#;
 
     /// Escape characters that may have special meaning in a shell,
     /// including spaces.
-    pub fn shell_escape(s: Cow<str>) -> Cow<str> {
+    pub fn escape(s: Cow<str>) -> Cow<str> {
         let escape_char = '\\';
         // check if string needs to be escaped
         let clean = SHELL_SPECIAL.chars().all(|sp_char| !s.contains(sp_char));
@@ -103,13 +104,13 @@ mod other {
     }
 
     #[test]
-    fn test_shell_escape() {
-        assert_eq!(shell_escape("--aaa=bbb-ccc".into()), "--aaa=bbb-ccc");
-        assert_eq!(shell_escape("linker=gcc -L/foo -Wl,bar".into()),
-                                r#"linker=gcc\ -L/foo\ -Wl,bar"#);
-        assert_eq!(shell_escape(r#"--features="default""#.into()),
-                                r#"--features=\"default\""#);
-        assert_eq!(shell_escape(r#"'!\$`\\\n "#.into()),
-                                r#"\'\!\\\$\`\\\\\\n\ "#);
+    fn test_escape() {
+        assert_eq!(escape("--aaa=bbb-ccc".into()), "--aaa=bbb-ccc");
+        assert_eq!(escape("linker=gcc -L/foo -Wl,bar".into()),
+                          r#"linker=gcc\ -L/foo\ -Wl,bar"#);
+        assert_eq!(escape(r#"--features="default""#.into()),
+                          r#"--features=\"default\""#);
+        assert_eq!(escape(r#"'!\$`\\\n "#.into()),
+                          r#"\'\!\\\$\`\\\\\\n\ "#);
     }
 }